package cn.mujiankeji.mbrowser.数据.数据库

import androidx.room.*
import cn.mujiankeji.mbrowser.数据.枚举类.快照类型枚举
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable



@Entity
data class DBQzm(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    var id: Long = 0L,

    // 所述页面包
    @ColumnInfo(name = "bid", defaultValue = "")
    val bid: String = "",

    // 轻页面的名称
    @ColumnInfo(name = "name", defaultValue = "")
    val 名称: String = "",

    // 匹配的地址 -- 正则
    @ColumnInfo(name = "url", defaultValue = "")
    val 匹配地址: String = "",

    @ColumnInfo(name = "type")
    val 类型 : 快照类型枚举,

    @ColumnInfo(name = "rule")
    val 规则内容: String = "", // 存储轻页面JSON或E2规则JSON

    @ColumnInfo(name = "enabled")
    val 启用: Boolean = true,

    @ColumnInfo(name = "time")
    val 更新时间: Long = System.currentTimeMillis()
)

// 简要规则数据类
// 注意：Room支持枚举自动映射为字符串
// 如果需要自定义映射可加TypeConverter

@Serializable
data class 快照规则简要(
    @ColumnInfo(name = "id")
    var id: Long = 0L,

    // 所述页面包
    @ColumnInfo(name = "bid", defaultValue = "")
    val bid: String = "",

    @ColumnInfo(name = "name", defaultValue = "")
    val 名称: String = "",

    // 匹配的地址 -- 正则
    @ColumnInfo(name = "url", defaultValue = "")
    val 匹配地址: String = "",

    @ColumnInfo(name = "type", defaultValue = "列表")
    val 类型 : 快照类型枚举 = 快照类型枚举.列表,

    // 轻页面的名称



)

@Dao
interface DaoQzm {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun 插入规则(规则: DBQzm): Long

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun 批量插入规则(规则列表: List<DBQzm>)

    @Update
    suspend fun 更新规则(规则: DBQzm)

    @Delete
    suspend fun 删除规则(规则: DBQzm)

    // 获取总数
    @Query("SELECT COUNT(*) FROM DBQzm")
    suspend fun 获取规则总数(): Int

    // 清空
    @Query("DELETE FROM DBQzm")
    suspend fun 清空()

    @Query("SELECT * FROM DBQzm WHERE id = :编号")
    suspend fun 查询规则ById(编号: Long): DBQzm?

    @Query("SELECT * FROM DBQzm WHERE enabled = 1")
    fun 获取所有启用规则(): Flow<List<DBQzm>>

    @Query("SELECT * FROM DBQzm WHERE bid = :bid")
    fun 获取所有规则(bid : Long): Flow<List<DBQzm>>

    // 新增：根据区块ID查询规则
    @Query("SELECT * FROM DBQzm WHERE bid = :区块ID")
    suspend fun 查询区块规则(区块ID: String): List<DBQzm>

    // 删除指定bid的所有规则
    @Query("DELETE FROM DBQzm WHERE bid = :bid")
    suspend fun 删除合集(bid: String)

    // 新增：只查必要字段的简要规则列表
    @Query("SELECT id,bid,name,url,type FROM DBQzm WHERE enabled = 1")
    suspend fun 获取所有启用规则简要(): List<快照规则简要>

    @Query("SELECT id,bid,name,url,type FROM DBQzm")
    suspend fun 获取所有规则简要(): List<快照规则简要>

    @Query("SELECT id,bid,name,url,type FROM DBQzm WHERE bid = :bid")
    fun 获取所有规则简要(bid : String): Flow<List<快照规则简要>>

    // 新增：根据区块ID查询规则
    @Query("SELECT id,bid,name,url,type FROM DBQzm WHERE bid = :区块ID")
    suspend fun 查询区块规则简要(区块ID: String): List<快照规则简要>

    @Query("SELECT COUNT(*) FROM DBQzm WHERE name = :it")
    suspend fun 检测名称可否使用(it: String): Int

}

class 快照类型转换器 {
    @TypeConverter
    fun from轻页面分类(value: 快照类型枚举?): String? = value?.name

    @TypeConverter
    fun to轻页面分类(value: String?): 快照类型枚举? = value?.let { 快照类型枚举.valueOf(it) }
} 